﻿using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.Mvc;
using KMF.WebUI.Areas.KMFadmin.Attributes;
using KMF.Common;
using KMF.DAL;
using KMF.WebUI.Areas.KMFadmin.CustomHelpers;
using KMF.WebUI.Areas.KMFadmin.Models;
using KMF.WebUI.Areas.KMFadmin.Infrastructure;

namespace KMF.WebUI.Areas.KMFadmin.Controllers
{
    [AdminLoggedAuthorize]
    public class AdminProviderController : Controller
    {
        //
        // GET: /Admin/Provider/

        public ActionResult Index(int page = 1)
        {
            var itemsPerPage = int.Parse(ConfigurationManager.AppSettings["DefaultItemsPerPage"]);
            var totalPage = 0;
            var model = ProviderDb.GetAllProvider(page, itemsPerPage, ref totalPage) ?? new List<ProviderListDto>();
            ViewBag.PageInfo = new PageInfo { CurrentPage = page, PageSize = itemsPerPage, TotalPages = totalPage };
            return View(model);
        }

        public JsonResult DeleteProvider(int id)
        {
            var result = ProviderDb.Delete(id);
            return Json(result > 0, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Add(ProviderDto model, HttpPostedFileBase fileImagePath)
        {
            var error = string.Empty;
            var imagePath = string.Empty;
            var folderUpload = Server.MapPath(ConstParam.FolderUploadProviderImage);
            if (fileImagePath != null) imagePath = FunctionHelper.UploadImages(out error, folderUpload, fileImagePath);
            model.ImagePath = imagePath;
            if (string.IsNullOrEmpty(model.Ordering.ToString()))
            {
                model.Ordering = 1;
            }
            var result = ProviderDb.Add(model);
            if (result > 0)
                return RedirectToAction("Index", "AdminProvider");
            return View(model);
        }

        public ActionResult Edit(int id)
        {
            var model = ProviderDb.GetById(id);
            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(ProviderDto model, HttpPostedFileBase fileImagePath)
        {
            var error = string.Empty;
            var imagePath = string.Empty;
            var folderUpload = Server.MapPath(ConstParam.FolderUploadProviderImage);
            if (fileImagePath != null)
            {
                imagePath = FunctionHelper.UploadImages(out error, folderUpload, fileImagePath);
                model.ImagePath = imagePath;
            }
            var result = ProviderDb.Update(model);
            if (result > 0) TempData["updateInfo"] = 1;
            else TempData["updateInfo"] = 0;
            //return View(model);
            return RedirectToAction("Index", "AdminProvider");
        }

        public ActionResult SearchProvider(string keyword = "", int page = 1)
        {
            if(string.IsNullOrEmpty(keyword))
            {
                keyword = "%";
            };
            var itemsPerPage = int.Parse(ConfigurationManager.AppSettings["DefaultItemsPerPage"]);
            var totalPage = 0;
            var model = ProviderDb.GetAllProviderByKeyWordAdmin(page, itemsPerPage, ref totalPage, keyword) ?? new List<ProviderListDto>();
            ViewBag.Keyword = keyword;
            ViewBag.PageInfo = new PageInfo { CurrentPage = page, PageSize = itemsPerPage, TotalPages = totalPage };
            return View(model);
        }
    }
}
